package com.right.health.app.ui.me.adapter;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.basic.adapter.BaseAbsAdapter;
import com.right.health.app.R;
import com.right.health.app.able.ISampleTextWatcher;
import com.right.health.app.bean.DrugReminderInfo;

import java.util.List;

/*
 * Created by hopeliao on 2019-09-24
 *
 */
public class DrugReminderAddAdapter extends BaseAbsAdapter<DrugReminderInfo> implements View.OnTouchListener, View.OnFocusChangeListener {

    public int mSelectedEditTextPosition = -1;

    private View.OnClickListener mClickListener;

    private MyTextWatcher mTextWatcher;

    public void setOnClickListener(View.OnClickListener listener) {
        this.mClickListener = listener;
    }

    public List<DrugReminderInfo> getDataSource() {
        return mDataSource;
    }

    public DrugReminderAddAdapter(Context context) {
        super(context);

        mTextWatcher = new MyTextWatcher();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final DrugReminderInfo info = mDataSource.get(position);

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.drug_reminder_add_item, null);
            viewHolder = new ViewHolder();
            viewHolder.chooseCycleTv = (TextView) convertView.findViewById(R.id.tv_choose_cycle);
            viewHolder.chooseTimeTv = (TextView) convertView.findViewById(R.id.tv_choose_time);
            viewHolder.startTimeTv = (TextView) convertView.findViewById(R.id.tv_start_time);
            viewHolder.endTimeTv = (TextView) convertView.findViewById(R.id.tv_end_time);
            viewHolder.nameEt = (EditText) convertView.findViewById(R.id.et_name);
            viewHolder.delTv = (TextView) convertView.findViewById(R.id.tv_del);

            convertView.setTag(viewHolder);

            if (mClickListener != null) {
                viewHolder.chooseCycleTv.setOnClickListener(mClickListener);
                viewHolder.chooseTimeTv.setOnClickListener(mClickListener);
                viewHolder.startTimeTv.setOnClickListener(mClickListener);
                viewHolder.endTimeTv.setOnClickListener(mClickListener);
                viewHolder.delTv.setOnClickListener(mClickListener);
            }
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.chooseCycleTv.setTag(position);
        viewHolder.chooseTimeTv.setTag(position);
        viewHolder.startTimeTv.setTag(position);
        viewHolder.endTimeTv.setTag(position);
        viewHolder.delTv.setTag(position);
        viewHolder.nameEt.setTag(position);

        viewHolder.nameEt.addTextChangedListener(mTextWatcher);
        viewHolder.nameEt.setOnTouchListener(this);
        viewHolder.nameEt.setOnFocusChangeListener(this);

        viewHolder.startTimeTv.setText(info.starttime);
        viewHolder.endTimeTv.setText(info.endtime);

        mTextWatcher.mCurrIndex = position;
        viewHolder.nameEt.setText(info.remindname);

        if (mSelectedEditTextPosition != -1 && position == mSelectedEditTextPosition) { // 保证每个时刻只有一个EditText能获取到焦点
            viewHolder.nameEt.addTextChangedListener(mTextWatcher);
        } else {
            viewHolder.nameEt.removeTextChangedListener(mTextWatcher);
        }

        if (info.cycle > 0) {
            viewHolder.chooseCycleTv.setText(String.format("每天%s次", info.cycle));
        } else {
            viewHolder.chooseCycleTv.setText("");
        }

        viewHolder.chooseTimeTv.setText(info.cycletimes);
        return convertView;
    }

    private class MyTextWatcher extends ISampleTextWatcher {

        public int mCurrIndex;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (mSelectedEditTextPosition != -1 && mSelectedEditTextPosition == mCurrIndex) {
                DrugReminderInfo itemTest = (DrugReminderInfo) getItem(mSelectedEditTextPosition);
                itemTest.remindname = s.toString();

                mDataSource.set(mSelectedEditTextPosition, itemTest);
            } else {
                Log.e("TAG", "pos = " + mSelectedEditTextPosition + ",currIndex = " + mCurrIndex + ",str="+s.toString());
            }
        }
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            EditText editText = (EditText) v;
            mSelectedEditTextPosition = (int) editText.getTag();
            mTextWatcher.mCurrIndex = mSelectedEditTextPosition;
            editText.addTextChangedListener(mTextWatcher);
        }
        return false;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        EditText editText = (EditText) v;
        if (hasFocus) {
            mTextWatcher.mCurrIndex = (int) editText.getTag();
//            editText.addTextChangedListener(mTextWatcher);
        } else {
//            editText.removeTextChangedListener(mTextWatcher);
        }
    }

    private class ViewHolder {
        private TextView delTv;

        //服药周期
        private TextView chooseCycleTv;
        private TextView chooseTimeTv;
        private TextView startTimeTv, endTimeTv;
        private EditText nameEt;
    }
}
